package examstarter;

import gui.CustomJButton;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import structure.Course;
import structure.Exam;

public class GUIItem extends JPanel{
	private static final long serialVersionUID = 1L;
	private GUIStarterRight parent;
	private Course course;
	private Exam exam;
	private JLabel titleCourse;
	private JLabel titleExam;
	private JLabel numofques;
	private int numofquestion;
	private JLabel time;
	private JButton btStart;
	private JButton btRemove;
	private long cooldownTime;
	
	public GUIItem(GUIStarterRight parent, Course course, Exam exam, int num) {
		
		this.parent = parent;
		this.course = new Course();
		this.exam = new Exam();
		this.numofquestion = num;
		titleCourse = new JLabel();
		titleExam = new JLabel();
		numofques = new JLabel();
		time = new JLabel();
		setLayout(new BorderLayout());
		setOpaque(false);
		cooldownTime = -10000;
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new GridBagLayout());
		mainPanel.setOpaque(false);
		setBorder(new EmptyBorder(10, 10, 10, 10));
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0.2f;
		mainPanel.add(new JLabel("Khoá :"),c);
		c.gridx = 1;
		c.weightx = 0.8f;
		mainPanel.add(titleCourse,c);
		
		c.gridx = 0;
		c.gridy = 1;
		c.weightx = 0f;
		mainPanel.add(new JLabel("Đề :"),c);
		c.gridx = 1;
		mainPanel.add(titleExam,c);
		
		c.gridx = 0;
		c.gridy = 2;
		mainPanel.add(new JLabel("T.gian :"),c);
		c.gridx = 1;
		mainPanel.add(time,c);
		
		c.gridx = 0;
		c.gridy = 3;
		mainPanel.add(new JLabel("Số câu :"),c);
		c.gridx = 1;
		mainPanel.add(numofques,c);
		
		btStart = new CustomJButton("Bắt đầu thi", 120, 30, Color.WHITE,
				CustomJButton.LIGHTERBLUE, Color.DARK_GRAY, 2, false, true);
		btRemove = new CustomJButton("Xoá", 80, 30, Color.WHITE,
				CustomJButton.LIGHTERRED, Color.DARK_GRAY, 2, false, true);
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttonPanel.setOpaque(false);
		buttonPanel.add(btStart);
		buttonPanel.add(btRemove);
		
		add(mainPanel, BorderLayout.CENTER);
		add(buttonPanel,BorderLayout.SOUTH);
		
		setCourse(course);
		setExam(exam);
		
		btStart.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(GUIItem.this.parent == null){
					return;
				}
				GUIItem.this.setCooldown(GUIItem.this.exam.getExamTime());
				GUIItem.this.parent.startItem(GUIItem.this.course, GUIItem.this.exam);
				GUIItem.this.btStart.setEnabled(false);
				GUIItem.this.btRemove.setEnabled(false);
			}
		});
		
		btRemove.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(GUIItem.this.parent == null){
					return;
				}
				GUIItem.this.parent.removeItem(GUIItem.this.course, GUIItem.this.exam);
			}
		});
	}
	
	public int getNumofQuestion(){
		return this.numofquestion;
	}
	
	public long getCooldown(){
		return this.cooldownTime;
	}
	
	public void setCooldown(long time){
		this.cooldownTime = time;
		this.btStart.setText(timetoString(time));
	}
	
	private String timetoString(long e){
		long min,sec;
		min = e/60;
		sec = e - min*60;
		return min + ":" + sec;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
		if(course == null) return;
		this.titleCourse.setText(course.getCourseTitle());
	}

	public Exam getExam() {
		return exam;
	}

	public void setExam(Exam exam) {
		this.exam = exam;
		if(exam == null) return;
		this.titleExam.setText(exam.getTitle());
		this.time.setText(this.exam.getTimeString());
		this.numofques.setText(this.numofquestion + " câu hỏi");
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d;
		g2d = (Graphics2D) g;
		
		g2d.setRenderingHint(
			    RenderingHints.KEY_ANTIALIASING,
			    RenderingHints.VALUE_ANTIALIAS_ON);
		
		RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(0, 0, this.getWidth()-1, this.getHeight()-1, 15, 15);
		
		g2d.setColor(new Color(250, 250, 250, 255));
		g2d.fill(roundedRectangle);
		
		g2d.setColor(new Color(100, 100, 100, 150));
		g2d.draw(roundedRectangle);
		g2d.setRenderingHint(
			    RenderingHints.KEY_ANTIALIASING,
			    RenderingHints.VALUE_ANTIALIAS_DEFAULT);
		super.paintComponent(g);
	}
}
